﻿package Cain.pcxbook
{
	import Cain.MkMc.Bk;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.getDefinitionByName;
	import flash.display.DisplayObject;
	/**
	 * ...//載入圖片 並將他放到適合大小 v1.3
	 * @author Cain in Liontreval 2009
	 * 
	 * 宣告====================================
	 * new PcxAutoSize() extends Sprite
	 * 
	 * 屬性====================================
	 * W :Number 寬
	 * H :Number 高
	 * Full :Boolean 滿版
	 * isload:Boolean 已有圖了
	 * 
	 * 方法====================================
	 * setup(  寬:Number, 高:Number)
	 * load(圖片路徑:Steite,是否滿版:Boolen = true , 元件庫中loading的影片Class名:String = null)
	 * 
	 * 事件====================================
	 * Event.COMPLETE  圖載完
	 */
	public class  LoaderAutoSize extends Sprite
	{
		private var pcx_url:String;
		private var ww:Number;
		private var hh:Number;
		private var loadingClassName:String;
		private var mk:Bk;
		private var loader:Loader;
		private var full:Boolean;
		private var is_load:Boolean;
		public function LoaderAutoSize() 
		{
			init();
		}
		
		public function get W():Number 
		{
			return ww;
		}
		public function set W(_w:Number):void 
		{
			mk.width = _w;
			ww = _w;
			loader.scaleX = 1;
			loader.scaleY = 1;
			setSize();
		}
		public function get H():Number 
		{
			return hh;
		}
		public function set H(_h:Number):void 
		{
			mk.height = _h;
			hh = _h;
			loader.scaleX = 1;
			loader.scaleY = 1;
			setSize();
		}
		public function get Full():Boolean 
		{
			return full;
		}
		public function set Full(_full:Boolean):void
		{
			loader.scaleX = 1;
			loader.scaleY = 1;
			full = _full;
			setSize();
		}
		private function init():void
		{
			
		}
		/**
		 * 初始化建構
		 * @param	_ww 寬
		 * @param	_hh 高
		 * @param	_DOUBLE_CLICK 滑鼠雙擊
		 */
		public function setup(_ww:Number,_hh:Number,_DOUBLE_CLICK:Boolean = false):void 
		{
			
			ww = _ww;
			hh = _hh;
			MK_setup(_DOUBLE_CLICK);//遮罩初始化
			
		}
		//遮罩初始化
		private function MK_setup(_DOUBLE_CLICK:Boolean):void
		{
			/*mk = new Sprite();
			mk.graphics.beginFill(0, 0);
			mk.graphics.drawRect(0, 0, ww, hh);
			mk.graphics.endFill();*/
			mk = new Bk(ww, hh, 1, 0);
			
			addChild(mk);
			//mask = mk;
			loader = new Loader();
			addChild(loader);
			loader.doubleClickEnabled = _DOUBLE_CLICK;
			loader.mask = mk; //4/27改
			
		}
		public function load(_pcx_url:String,_full:Boolean = true,_loadingClassName:String = null):void 
		{
			try 
			{
				removeChild(getChildByName("loadingMC"));
				loader.close();
			}catch (err:Error)
			{
	
			}
			pcx_url = _pcx_url;
			full = _full;
			loadingClassName = _loadingClassName;
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
			loading_setup();//設定loading動畫
			
			try 
			{
				loader.load(new URLRequest(pcx_url));
			}catch (err:Error)
			{
				trace(err.message);
			}
			
		}
		//停止loading
		public function close():void 
		{
			loader.close();
			if (loadingClassName!=null) 
			{
				removeChild(getChildByName("loadingMC"));
			}
		}
		//設定loading動畫
		private function loading_setup():void
		{
			if (loadingClassName!=null) 
			{//如果loadingClassName有東西
				try 
				{
					var new_class:Class = getDefinitionByName(loadingClassName) as Class;
					var kk:DisplayObject = new new_class();
					kk.x = ww / 2;
					kk.y = hh / 2;
					kk.name = "loadingMC";
					addChild(kk);
				}catch (e:Error)
				{
					trace("元件庫中沒有" + loadingClassName);
				}
				
			}
		}
		//圖形載完
		private function loaded(e:Event):void 
		{
			//addChild(loader);
			/*try 
			{
				Bitmap(loader.content).smoothing = true;
			}catch (e:Error)
			{
				trace("內容不是點陣物件");
			}*/
			setSize();//設定圖大小
			if (loadingClassName!=null) 
			{
				removeChild(getChildByName("loadingMC"));
			}
			is_load = true;
			dispatchEvent(new Event(Event.COMPLETE));
		}
		/**
		 * 已經有圖了
		 * @return true 代表已載完一次了
		 */
		public function get isload():Boolean 
		{
			return is_load;
		}
		//設定圖大小
		private function setSize():void
		{
			//loader.content.x = -(loader.content.width >> 1);
			//loader.content.y = -(loader.content.height >> 1);
			var sc_w:Number = ww/loader.width;
			var sc_h:Number = hh / loader.height;
			if (full) 
			{//滿版
				if (sc_w>=sc_h) 
				{
					loader.scaleX = sc_w;
					loader.scaleY = sc_w;
				}else 
				{
					loader.scaleX = sc_h;
					loader.scaleY = sc_h;
				}
			}else 
			{//顯示整個
				if (sc_w<=sc_h) 
				{
					loader.scaleX = sc_w;
					loader.scaleY = sc_w;
				}else 
				{
					loader.scaleX = sc_h;
					loader.scaleY = sc_h;
				}
			}
			set_md();//置中
		}
		//置中
		private function set_md():void
		{
			var md_x:Number = ww / 2;
			var md_y:Number = hh / 2;
			loader.x = -((loader.width >> 1) - md_x);
			loader.y = -((loader.height >> 1) - md_y);
		}
		
		
	}
	
}